#!/usr/bin/env groovy

import groovy.json.JsonOutput
import groovy.json.JsonSlurper
//import org.json.simple.JSONObject
import groovy.transform.Field

//global variable
@Field String GITURL = "https://git.augmentum.com.cn"

def sendGetRequest(String apiUrl)
{
    //def connection = new URL("https://git.augmentum.com.cn/api/v4/users/1696").openConnection() as HttpURLConnection

    def connection = new URL("${GITURL}/${apiUrl}").openConnection() as HttpURLConnection
    //必须是GET方法
    connection.setRequestMethod("GET")
    connection.setRequestProperty('User-Agent', 'groovy-'.concat(GroovySystem.getVersion()))
    connection.setRequestProperty('Accept', 'application/json')
    //必须设置PRIVATE-TOKEN，不然返回404状态
    connection.setRequestProperty('PRIVATE-TOKEN', '1qfBxwCzmLRknsuFZQXs')
    def response = connection.inputStream.text
//    println(response.toString())
    return response
}

def jsonHandler(String result)
{
    //json处理
    def slurper = new JsonSlurper()
//    def jsonText = slurper.parseText(JsonOutput.prettyPrint(result.toString()))
    def jsonText = slurper.parseText(result.toString())
    def email=jsonText.name as String[]
    for(String i in email) {
        println(i);
    }

//    JSONObject jsonObj= JSONObject.fromObject(jsonText);
//    def value= jsonText.getString(key);
//    println(value);

}

def call(String apiUrl)
{
    def response= sendGetRequest (apiUrl)
    jsonHandler(response)
}

// call("/api/v4/groups/smartconstruction/members")






